package net.csdn.business.discuss.common.enums;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.util.Map;

/**
 * @author likun
 * @date 2022/6/6 17:57
 */
@Getter
@AllArgsConstructor
public enum ResultCodeEnum {
    /**
     * 成功
     */
    OK(200, "success", "MESSAGE.OK", HttpStatus.OK),
    SUCCESS(200, "success", "MESSAGE.SUCCESS", HttpStatus.OK),
    BAD_REQUEST(400, "Bad Request", "MESSAGE.BAD_REQUEST", HttpStatus.BAD_REQUEST),
    USER_PWD_ERROR(401, "账号或者密码错误", "MESSAGE.USER_PWD_ERROR", HttpStatus.UNAUTHORIZED),
    UNAUTHORIZED(403, "操作失败，暂无权限", "MESSAGE.UNAUTHORIZED", HttpStatus.FORBIDDEN),
    FEIGN_UNAUTHORIZED(403, "请求失败，feign请求token有误", "MESSAGE.FEIGN_UNAUTHORIZED", HttpStatus.FORBIDDEN),
    RESOURCE_NOT_EXIST(404, "资源不存在", "MESSAGE.RESOURCE_NOT_EXIST", HttpStatus.NOT_FOUND),
    NOT_PATH(404, "路径不存在", "MESSAGE.RESOURCE_NOT_EXIST", HttpStatus.NOT_FOUND),
    METHOD_NOT_ALLOWED(406, "Method Not Allowed", "MESSAGE.METHOD_NOT_ALLOWED", HttpStatus.METHOD_NOT_ALLOWED),

    /**
     * 重复提交
     */
    REPEAT_SUBMIT_ERROR(450, "重复提交", "MESSAGE.REPEAT_SUBMIT_ERROR", HttpStatus.BAD_REQUEST),

    FAIL(500, "系统错误", "MESSAGE.FAIL", HttpStatus.INTERNAL_SERVER_ERROR),
    UN_KNOW(500, "未知错误", "MESSAGE.FAIL", HttpStatus.BAD_REQUEST),
    SERVICE_UNAVAILABLE(503, "内部服务调用失败", "MESSAGE.SERVICE_UNAVAILABLE", HttpStatus.SERVICE_UNAVAILABLE),
    THIRD_ERROR(999, "接口错误，稍后重试", "MESSAGE.THIRD_ERROR", HttpStatus.INTERNAL_SERVER_ERROR),
    PARAMETER_ERROR(1000, "参数错误", "MESSAGE.PARAMETER_ERROR", HttpStatus.BAD_REQUEST),
    PARAMETER_ERROR_CUSTOM(1000, "%s参数错误", "MESSAGE.PARAMETER_ERROR_CUSTOM", HttpStatus.BAD_REQUEST),
    ILLEGAL_OPERATION(1001, "非法操作", "MESSAGE.ILLEGAL_OPERATION", HttpStatus.BAD_REQUEST),
    NOT_EXIST(1002, "资源不存在", "MESSAGE.NOT_EXIST", HttpStatus.NOT_FOUND),
    EMAIL_TEMPLATE_NOTE_EXIST(1003, "%s通知模板不存在", "MESSAGE.EMAIL_TEMPLATE_NOTE_EXIST", HttpStatus.BAD_REQUEST),
    AUTHING_ERROR(1004, "authing服务出错", "MESSAGE.AUTHING_ERROR", HttpStatus.BAD_REQUEST),
    NOTICE_YUN_ERROR(1005, "通知云效服务出错", "MESSAGE.NOTICE_YUN_ERROR", HttpStatus.INTERNAL_SERVER_ERROR),

    NOTNULL(1006, "%s不能为空", "MESSAGE.NOTNULL", HttpStatus.BAD_REQUEST),
    INVALID_VER_CODE(1007, "验证码不正确或已过期", "MESSAGE.INVALID_VER_CODE", HttpStatus.BAD_REQUEST),
    SEND_VER_CODE_ERROR(1007, "验证码发送失败", "MESSAGE.SEND_VER_CODE_ERROR", HttpStatus.BAD_REQUEST),
    /**
     * 讨论相关3001-3100
     */
    DISCUSS_CATEGORY_NO_EXIST_ERROR(3001,"讨论分类不存在","MESSAGE.DISCUSS_CATEGORY_NO_EXIST_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_ILLEGAL_ERROR(3002,"操作失败，暂无权限","MESSAGE.DISCUSS_COMMENT_ILLEGAL_ERROR", HttpStatus.FORBIDDEN),
    DISCUSS_REMARK_REPATE_ERROR(3003,"操作失败，重复操作","MESSAGE.DISCUSS_REMARK_REPATE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_NO_EXIST_ERROR(3004,"操作失败，讨论不存在","MESSAGE.DISCUSS_REMARK_REPATE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_CLOSE_ERROR(3005,"操作失败，讨论已关闭","MESSAGE.DISCUSS_REMARK_REPATE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_CATEGORY_UP_LIMIT_ERROR(3006,"操作失败，讨论分类数量超过限制","MESSAGE.DISCUSS_CATEGORY_UP_LIMIT_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_POLL_DISABLE_CHANGE_ERROR(3007,"操作失败，投票类型的讨论不允许修改为非投票类型","MESSAGE.DISCUSS_POLL_DISABLE_CHANGE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_VOTE_REPATE_ERROR(3008,"投票失败，重复投票","MESSAGE.DISCUSS_VOTE_REPATE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_VOTE_CHANGE_ONE_ERROR(3009,"投票失败，投票只允许修改一次","MESSAGE.DISCUSS_VOTE_CHANGE_ONE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_LOCK_ERROR(3010,"操作失败，讨论已经锁定","MESSAGE.DISCUSS_LOCK_VOTE_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_CATEGORY_CHANGE_POLL_ERROR(3011,"操作失败，非投票类型的讨论不允许修改为投票类型","MESSAGE.DISCUSS_CATEGORY_CHANGE_POLL_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_SECTION_NAME_EXIST_ERROR(3012,"操作失败，分组名称已存在","MESSAGE.DISCUSS_SECTION_NAME_EXIST_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_CATEGORY_NAME_EXIST_ERROR(3013,"操作失败，分类名称已存在","MESSAGE.DISCUSS_SECTION_NAME_EXIST_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_REMARK_ANSWER_ERROR(3014,"操作失败，此评论已删除，不能采纳为答案","MESSAGE.DISCUSS_REMARK_ANSWER_ERROR", HttpStatus.BAD_REQUEST),
    DISCUSS_AUDIT_FAIL_ERROR(3015,"讨论不存在","MESSAGE.DISCUSS_AUDIT_FAIL_ERROR",HttpStatus.NOT_FOUND),
    ;



    final int code;
    final String msg;
    final String i18n;
    final HttpStatus httpStatus;

    private static final Map<String, ResultCodeEnum> NAME_TO_CODE = Maps.newHashMap();

    static {
        for (ResultCodeEnum value : ResultCodeEnum.values()) {
            NAME_TO_CODE.put(value.name(), value);
        }
    }

    public static String format(ResultCodeEnum resultCodeEnum, String field) {
        return String.format(resultCodeEnum.getMsg(), field);
    }

    public static ResultCodeEnum getResultCode(String name) {
        return NAME_TO_CODE.getOrDefault(name, FAIL);
    }
}